﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using TRPOP.CMS.Plugin;
using System.Text;
using System.Security.Cryptography;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Xml.Serialization;
using TRPOP.Common;

namespace Yorkg.CMS.OtherWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnClick_Click(object sender, EventArgs e)
        {
            if (txtName.Text.IsEmpty())
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", string.Format("<script>alert('{0}')</script>", "请输入企业名称"));
                return;
            }
            AuthorizationInfo oAuthorizationInfo = new AuthorizationInfo
            {
                SoftVersion = ddlVersion.SelectedValue.ToInt(),
                EnterpriseLiceTime = DateTime.Now,
                EnterpriseName = txtName.Text.Trim(),
                EnterpriseLiceNum = MD5(System.Guid.NewGuid() + txtName.Text.Trim() + System.Guid.NewGuid())
            };
            TRPOP.Common.FileUtil.CheckPathAndCreate(Server.MapPath("/" + txtKey.Text), true);
            string strTest = SerializeObj(oAuthorizationInfo, "/"+txtKey.Text+"/license", oAuthorizationInfo.EnterpriseLiceNum);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", string.Format("<script>alert('{0}')</script>", "创建成功"));
        }

        /// <summary>
        /// MD5函数
        /// </summary>
        /// <param name="str">原始字符串</param>
        /// <returns>MD5结果</returns>
        public static string MD5(string str)
        {
            byte[] b = Encoding.Default.GetBytes(str);
            b = new MD5CryptoServiceProvider().ComputeHash(b);
            string ret = "";
            for (int i = 0; i < b.Length; i++)
                ret += b[i].ToString("x").PadLeft(2, '0');
            return ret;
        }

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <param name="oAuthorizationInfo"></param>
        /// <returns></returns>
        public static string SerializeObj(AuthorizationInfo oAuthorizationInfo, string fileName, string strKey)
        {
            string strPath = HttpContext.Current.Server.MapPath(fileName);
            SerializationHelper.Save(oAuthorizationInfo, strPath);
            if (System.IO.File.Exists(strPath))
            {
                StreamReader oSR = new StreamReader(strPath);
                string strAllstr = oSR.ReadToEnd();
                oSR.Close();
                oSR.Dispose();
                // 对授权码des，
                //
                strAllstr = "!ZLXYCJY" + strAllstr + "!G^J&";//混淆字符串
                strAllstr = StringExtension.DESEncode(strAllstr, "TRPOP_Workshop".MD5Hash());
                if (System.IO.File.Exists(strPath))
                {
                    using (System.IO.StreamWriter strW = new System.IO.StreamWriter(strPath, false))
                    {
                        strW.WriteLine(strAllstr);
                        strW.Close();
                    }
                }
                return strAllstr;
            }
            return "";
        }
    }
}